package com.example.huawei.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.huawei.system.entity.SysUser;
import com.example.huawei.system.entity.SysUserLogin;
import com.example.huawei.system.entity.SysUserRegister;
import com.example.huawei.system.result.Result;
import com.example.huawei.system.service.SysUserService;
import com.example.huawei.system.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Api(tags = "登录")
@RestController
@RequestMapping("/server")
public class LoginController {


    @Autowired
    private SysUserService userService;

    @ApiOperation(value = "注册帐号", notes = "注册帐号")
    @PostMapping("/register")
    public Result<?> register(@RequestBody SysUserRegister sysUserRegister) {

        SysUser sysUser = userService.lambdaQuery().eq(SysUser::getAccount, sysUserRegister.getAccount()).one();
        if (sysUser != null) {
            return Result.error().message("帐号已存在");
        }
        if (!sysUserRegister.getPassword().equals(sysUserRegister.getConfirmPassword())) {
            return Result.error().message("两次输入的密码不一致");
        }
        //如果不存在此用户则新增 存在则更新
        sysUser = new SysUser();
        sysUser.setUsername(sysUserRegister.getUsername());
        if (StringUtils.isEmpty(sysUserRegister.getAvatar())) {
            sysUser.setAvatar("/images/2025/08/27/7ae0002478474b688f0c6b8489ed5ed7.png");
        }else {
            sysUser.setAvatar(sysUserRegister.getAvatar());
        }
        sysUser.setSex(sysUserRegister.getSex());
        sysUser.setAccount(sysUserRegister.getAccount());
        sysUser.setPassword(sysUserRegister.getPassword());
        sysUser.setCreateTime(new Date());
        boolean flag = userService.save(sysUser);

        return flag ? Result.ok() : Result.error();
    }

    @ApiOperation(value = "用户登录", notes = "用户登录")
    @PostMapping("/login")
    public Result<?> login(@RequestBody SysUserLogin sysUserLogin) {

        SysUser sysUser = userService.lambdaQuery()
                .eq(SysUser::getAccount, sysUserLogin.getAccount())
                .eq(SysUser::getPassword, sysUserLogin.getPassword())
                .one();
        //如果不存在此用户则新增 存在则更新
        if (sysUser == null) {
            return Result.error().message("帐号或密码错误");
        }

        JSONObject result = new JSONObject();
        sysUser.setToken(JwtUtil.generateToken(sysUser.getAccount() + sysUser.getPassword()));
        result.put("userInfo", sysUser);

        return Result.ok(result);
    }


//    @ApiOperation(value = "获取token", notes = "返回数据为token的接口")
//    @PostMapping("/login")
//    public Result login(@RequestBody SysUserLogin sysUserLogin) {
//        String accessToken = HwUtils.getAccessToken(sysUserLogin.getCode());
//        if (Objects.isNull(accessToken)) {
//            return Result.error().message("获取用户级凭证失败!");
//        }
//        JSONObject userInfo = HwUtils.login(accessToken);
//        if (Objects.isNull(userInfo)) {
//            return Result.error().message("获取用户级凭证失败!");
//        }
//        String unionID = userInfo.getString("unionID");
//        SysUser sysUser = userService.lambdaQuery().eq(SysUser::getUnionId, unionID).one();
//        //如果不存在此用户则新增 存在则更新
//        if (sysUser == null) {
//            sysUser = new SysUser();
//        }
//        sysUser.setOpenId(userInfo.getString("openID"));
//        sysUser.setUnionId(userInfo.getString("unionID"));
//        sysUser.setLoginMobileNumber(userInfo.getString("loginMobileNumber"));
//        sysUser.setLoginMobileValid(userInfo.getInteger("loginMobileValid"));
//        sysUser.setPurePhoneNumber(userInfo.getString("purePhoneNumber"));
//        sysUser.setPhoneCountryCode(userInfo.getString("phoneCountryCode"));
//        sysUser.setUsername(sysUserLogin.getUsername());
//        sysUser.setAvatar(sysUserLogin.getAvatar());
//        sysUser.setSex(sysUserLogin.getSex());
//        sysUser.setCreateTime(new Date());
//        userService.saveOrUpdate(sysUser);
//        JSONObject result = new JSONObject();
//        result.put("token", JwtUtil.generateToken(sysUser.getUnionId()));
//        result.put("userInfo", sysUser);
//
//        return Result.ok(result);
//    }


}
